package com.shopping_assistant.model;

import java.util.Comparator;

public class OfferViewCounter {

	private Person person;
	private Offer offer;
	private int totalViews;
	
	public OfferViewCounter(Person person, Offer offer) {
		this.person = person;
		this.offer = offer;
		totalViews = 0;
	}
	
	public OfferViewCounter(Person person, Offer offer, int totalViews) {
		this.person = person;
		this.offer = offer;
		this.totalViews = totalViews;
	}
	
	public Person getPerson() {
		return person;
	}

	public Offer getOffer() {
		return offer;
	}

	public int getTotalViews() {
		return totalViews;
	}
	
	public void countView() {
		totalViews++;
	}
	
	public static Comparator<OfferViewCounter> compareByViews() {
		return new sortOffers();
	}
	
	private static class sortOffers implements Comparator<OfferViewCounter> {

		public int compare(OfferViewCounter lhs, OfferViewCounter rhs) {
			return rhs.getTotalViews() - lhs.getTotalViews();
		}
	}
	
	@Override
	public String toString() {
		return offer.getTitle() + " [" + totalViews + "]";
	}
}
